<?php
/**
 * Created by PhpStorm.
 * User: zjx
 * Date: 2016/11/25
 * Time: 10:32
 */

namespace App\Services\Utils;
use Illuminate\Support\Str;


class ArrKeySnaker
{
    static protected $customer_key_map = [
        'EBusinessID' => 'e_business_id'
    ];

    static public function transform(array $assoc_array)
    {
        return static::snakeAssocKeys($assoc_array);
    }

    static protected function snakeAssocKeys(array $assoc_array)
    {
        $keys = array_keys($assoc_array);
        $values = array_values($assoc_array);

        $format_keys = static::snakeKeys($keys);
        $format_values = array_map(function ($val) {
            if (is_object($val)) {
                $val = (array) $val;
            }
            if (is_array($val)) {
                $val = static::snakeAssocKeys($val);
            }
            return $val;
        }, $values);

        $format_data = array_combine($format_keys, $format_values);

        return $format_data;
    }

    static protected function snakeKeys(array $array_keys)
    {
        return array_map(function ($key_name) {
            if (array_key_exists($key_name, static::$customer_key_map)) {
                return static::$customer_key_map[$key_name];
            }
            return Str::snake($key_name);
        }, $array_keys);
    }
    
}